package com.oromwin.repos.server.request.file;

import java.io.DataInputStream;
import java.io.IOException;
import java.util.List;

import org.apache.commons.logging.Log;

import com.oromwin.repos.common.Logger;
import com.oromwin.repos.common.ReposException;
import com.oromwin.repos.common.helper.DBHelper;
import com.oromwin.repos.common.helper.IOHelper;
import com.oromwin.repos.data.AbstractReposTable;
import com.oromwin.repos.data.FileTable;
import com.oromwin.repos.server.LockObject;
import com.oromwin.repos.server.request.AbstractRequest;
import com.oromwin.repos.server.response.NormalResponse;
import com.oromwin.repos.server.response.Response;

public class RenameFileRequest extends AbstractRequest {
    private Log log = Logger.getLog(RenameFileRequest.class);
    private String FileId = null;
    private String FileName = null;
    
    @Override
    public void receive(DataInputStream dis, LockObject lockObject) throws ReposException {
        try {
            synchronized (lockObject) {
                FileId = lockObject.getFileId();
                FileName = IOHelper.readUTF8(dis);
            }
        } catch (IOException e) {
            String message = String.format("Failed to read stream, {%s}", e.getMessage());
            throw ReposException.create(log, e, message);
        }
    }

    @Override
    public Response run() throws ReposException {
        NormalResponse res = null;
        try {
            getCurrentReposTables();
            String whereClause = String.format("FileId = '%s'", FileId);
            String prevName = _fileTable.getFileName();
            _fileTable.setFileName(FileName);
            DBHelper.updateTable(_fileTable, whereClause);
            res = new NormalResponse(true, String.format("Succeeded to rename filename '%s' to '%s'", prevName, FileName));
        } catch (ReposException e) {
            res = new NormalResponse(false, e.getMessage());
        } catch (Exception e) {
            String message = String.format("Unexpected exception, {%s}", e.getMessage());
            throw ReposException.create(log, e, message);
        }
        return res;
    }

    /**
     * Loading file-info from database
     */
    @Override
    public void getCurrentReposTables() throws ReposException {
        AbstractReposTable reposTable = null;
        String whereClause = String.format("FileId = '%s' AND FileStatus = 0", FileId);
        List<AbstractReposTable> reposTables = DBHelper.selectTable(FileTable.class, whereClause);
        if (reposTables != null && reposTables.size() == 1) {
            reposTable = reposTables.get(0);
        } else {
            String message = String.format("'%s' FileId does not exists in database, tableSize:whereClause {%d:%s}", FileId, reposTables.size(), whereClause);
            throw ReposException.create(log, message); 
        }
        if (reposTable instanceof FileTable) {
            _fileTable = (FileTable) reposTable;
        } else {
            String message = String.format("Illegal file table type, class {%s}", reposTable.getClass().toString());
            throw ReposException.create(log, message);
        }
    }

}
